<?php session_start();
if($_POST['page'])
{
$page = $_POST['page'];
$cur_page = $page;
$page -= 1;
$per_page = 5;
$previous_btn = true;
$next_btn = true;
$first_btn = true;
$last_btn = true;
$start = $page * $per_page;
include"functions.php";
$member_id = $_SESSION['member_id'];
$query_pag_data = "SELECT m.first_name, m.last_name, ms.subject, 
s.message_id, ms.date_sent, ms.seen, ms.message, s.conference_id FROM member m
         INNER JOIN member_message_member s
         ON (m.member_id = s.member_id1)
         INNER JOIN messages ms ON s.message_id=ms.message_id
            WHERE s.member_id2='$member_id'
                ORDER BY ms.date_sent DESC
LIMIT $start, $per_page";
$result_pag_data = mysql_query($query_pag_data) or die('MySql Error' . mysql_error());
$msg = "";
$msg .= "<table id='containerT' class='no_border'>
    <tr><td style=' background-color:#003366; color:snow;' width='11%'><input type='checkbox' id='all' name='all'/>
        <label class='cursor_pointer' for='all'>Check all</label></td>
        <td style='text-align:center; background-color:#003366; color:snow;' width='12.5%'>From</td>
              <td style='text-align:center; background-color:#003366; color:snow;' width='17%'>Conference</td>
              <td style='text-align:center; background-color:#003366; color:snow;' width='45%'>View Message</td>
              <td style='text-align:center; background-color:#003366; color:snow;' width='12.5%'>Sent at</td>
              <td style='text-align:center; background-color:#003366; color:snow;' width='2%'>Delete</td>
    </tr>";
while ($row = mysql_fetch_assoc($result_pag_data)) {
                if ($row['seen'] == "0") {
                    $f_name = formatText_safe($row['first_name']);
                    $l_name = formatText_safe($row['last_name']);
                    $subject = formatText_safe($row['subject']);
                    $safeMsg = formatText_safe($row['message']);
                    if(strlen($subject) > 20)
                    $subject = substr ($subject, 0, 20) . "...";
                    $mss = strval($safeMsg);
                    $m = substr($mss, 0, 40) . '...';
                    $id = $row['message_id'];
                    $name = get_conference_name($row['conference_id']);
                    $msg .=  "<table class='unread_message'>
                <tr><td width='11%'><input type='checkbox' name='ids[]' value='$id' /></td>
                    <td width='12.5%'>{$f_name} {$l_name}</td>
                    <td width='17%'>$name</td>
                    <td width=45%' style='cursor:pointer;' class='sub' link='viewMessage.php?message_id={$row['message_id']}' msg_id='{$row['message_id']}' mID='$member_id'>
                    <div>
                    {$subject} - <span style='color:grey;'>$m</span></div></td>
                    <td width='12.5%'>{$row['date_sent']}</td>
                    <td width='2%' style='text-align:center;'><a title='delete' mID='$member_id' msg_id='{$row['message_id']}' class='delete' href='#'></a></td>
                    </tr></table>";
                } else if ($row['seen'] == "1") {
                    $f_name = formatText_safe($row['first_name']);
                    $l_name = formatText_safe($row['last_name']);
                    $subject = formatText_safe($row['subject']);
                    $safeMsg = formatText_safe($row['message']);
                    if(strlen($subject) > 20)
                    $subject = substr ($subject, 0, 20) . "...";
                    $mss = strval($safeMsg);
                    $m = substr($mss, 0, 40) . '...';
                    $name = get_conference_name($row['conference_id']);
                    $id = $row['message_id'];
                    $msg .= "<table class='seen_message'>
                <tr><td width='11%'><input type='checkbox' name='ids[]' value='$id' /></td>
                    <td width='12.5%'>{$f_name} {$l_name}</td>
                    <td width='17%'>$name</td>
                    <td width='45%' style='cursor:pointer;' class='sub' link='viewMessage.php?message_id={$row['message_id']}' msg_id='{$row['message_id']}'
                    mID='$member_id'><div>
                    {$subject} - <span style='color:grey'>$m</span></div></td>
                    <td width='12.5%'>{$row['date_sent']}</td>
                    <td width='2%' style='text-align:center;'><button title='delete' class='delete' msg_id={$row['message_id']}></button></td>

                </tr></table>";
                }
            }


$query_pag_num = "SELECT COUNT(*) AS count FROM member m
         INNER JOIN member_message_member s
         ON (m.member_id = s.member_id1)
         INNER JOIN messages ms ON s.message_id=ms.message_id
            WHERE s.member_id2='$member_id'";
$result_pag_num = mysql_query($query_pag_num) or die(mysql_error());
$row = mysql_fetch_array($result_pag_num);
$count = $row['count'];
$no_of_paginations = ceil($count / $per_page);

if ($cur_page >= 7) {
    $start_loop = $cur_page - 3;
    if ($no_of_paginations > $cur_page + 3)
        $end_loop = $cur_page + 3;
    else if ($cur_page <= $no_of_paginations && $cur_page > $no_of_paginations - 6) {
        $start_loop = $no_of_paginations - 6;
        $end_loop = $no_of_paginations;
    } else {
        $end_loop = $no_of_paginations;
    }
} else {
    $start_loop = 1;
    if ($no_of_paginations > 7)
        $end_loop = 7;
    else
        $end_loop = $no_of_paginations;
}
if(intval($count) != 0) {
$msg .= "<div class='pagination'><ul>";
$msg .= "<table  width='99%' class='no_border'>";
$msg .= "<tr><td style='text-align:left;' width='5%'><div id='loading'></div></td>";
if ($first_btn && $cur_page > 1) {
    $msg .= "<td width='74%'><li p='1' class='active'>First</li>";
} else if ($first_btn) {
    $msg .= "<td width='74%'><li p='1' class='inactive'>First</li>";
}

if ($previous_btn && $cur_page > 1) {
    $pre = $cur_page - 1;
    $msg .= "<li p='$pre' class='active'>Previous</li>";
} else if ($previous_btn) {
    $msg .= "<li class='inactive'>Previous</li>";
}
for ($i = $start_loop; $i <= $end_loop; $i++) {

    if ($cur_page == $i)
        $msg .= "<li p='$i' style='color:#fff;background-color:#003366;' class='active'>{$i}</li>";
    else
        $msg .= "<li p='$i' class='active'>{$i}</li>";
}

if ($next_btn && $cur_page < $no_of_paginations) {
    $nex = $cur_page + 1;
    $msg .= "<li p='$nex' class='active'>Next</li>";
} else if ($next_btn) {
    $msg .= "<li class='inactive'>Next</li>";
}
if ($last_btn && $cur_page < $no_of_paginations) {
    $msg .= "<li p='$no_of_paginations' class='active'>Last</li>";
} else if ($last_btn) {
    $msg .= "<li p='$no_of_paginations' class='inactive'>Last</li></td>";
}

$goto = "<td width='10%'><span>Go to <input type='text' class='goto' size='1'/></span></td>
    ";
$total_string = "<td width='10%'><span class='total' a='$no_of_paginations'>Page <b>" . $cur_page . "</b> of <b>$no_of_paginations</b></span></td></tr>";
$msg = $msg . "</ul>" . $goto . $total_string . "</table></div>";  
} else {
    $msg .= "<td colspan='10'><p align='center'>You do not have messages.</p></td>";
}
echo $msg;
}